package com.yanyuhao.exception;

//全局异常捕获器

import com.yanyuhao.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice("com.yanyuhao.controller")
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody //返回json串
    public Result error(Exception e) {
        log.error("系统异常",e);
        return Result.error("系统异常");
    }

    @ExceptionHandler(CustomerException.class)
    @ResponseBody //返回json串
    public Result customerError(CustomerException e) {
        log.error("自定义错误",e);
        return Result.error(e.getCode(),e.getMsg());
    }

}


